﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;


namespace ConcreteMappersAndObjects.Mappers
{
    public class TeachersDataMapper : AbstractDataMapper<int, Teacher>
    {
        private IEntityMapper<TeacherClassCUnitKey, TeacherClassCUnit> _tccu;

        public void SetTeacherClassCUnitMapper(IEntityMapper<TeacherClassCUnitKey, TeacherClassCUnit> mapper)
        {
            _tccu = mapper;
        }
        protected override int GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "select ID, Name from Teachers";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ID = @ID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        private class TeacherFilter : IFilter
        { 
            private int _key;
            public TeacherFilter(int key) { _key = key; }


            #region IFilter Members

            public string GetWhereClause()
            {
               return  " where TeacherID = @TeacherID";
            }

            public void BindValues(System.Data.SqlClient.SqlCommand cmd)
            {
                cmd.Parameters.AddWithValue("@TeacherID", _key);
            }

            #endregion
        }


        protected override Teacher DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            int key = GetKeyFrom(sdr);
            LazySet<TeacherClassCUnitKey, TeacherClassCUnit> cuClasses =
                new LazySet<TeacherClassCUnitKey,TeacherClassCUnit>(_tccu, new TeacherFilter(key));
            return new Teacher(key, sdr.GetString(1), cuClasses);
        }

        protected override string GetUpdateCommand()
        {
            throw new NotImplementedException();
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, Teacher t)
        {
            throw new NotImplementedException();
        }

        protected override string GetInsertCommand()
        {
            return "TeacherInsert";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, Teacher t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 100).Value = t.Name;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from Teachers where ID = @ID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, Teacher t)
        {
            cmd.Parameters.AddWithValue("@ID", t.ID);
        }

        protected override void FinishUpdateObject(Teacher t)
        {
            
        }

        protected override void FinishNewObject(Teacher t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Teacher t)
        {
            
        }
    }
}
